package com.lvbaoins.creditapp.service;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lvbaoins.creditapp.util.HttpClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author : gjb
 * @description:
 * @date : 2020-11-27 15:36
 * @version: : v1.0
 */
@Slf4j
@Service
public class RequestService {


    /**
     * 最终拼装
     *
     * @param reqUrl
     * @param params
     * @return
     */
    public JSONObject sendHttpRequest(String mallId, String appKey, String reqUrl, Map<String, Object> params) {
        Map<String, Object> sendMap = new LinkedHashMap<>();
        sendMap.put("mall_id", mallId);
        //sendMap.put("agentwe_mobile")
        sendMap.putAll(params);
        sendMap.put("tm", LocalDateTime.now().toInstant(ZoneOffset.of("+8")) + "000");
        String sign = createSign(appKey, sendMap);
        sendMap.put("sign", sign);
        if (!reqUrl.toLowerCase().contains("upload")) {
            log.info("发送地址:{},发送数据:{}", reqUrl, JSON.toJSONString(sendMap));
        } else {
            log.info("发送地址:{},发送数据(file):{}", reqUrl, sendMap.get("desc"));
        }
        String res = HttpClient.doGet(reqUrl, sendMap);
       /* Map<String, String> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功");
        result.put("status", "");
        String res = JSON.toJSONString(result);*/
        log.info("返回数据:" + res);
        if (StringUtils.isBlank(res)) {
            log.info("返回数据:为null");
            return null;
        } else {
            return JSONObject.parseObject(res);
        }
    }

    /**
     * @Title: createSign
     * @Description: 生成签名
     * @param: sendJson
     * @return: String
     */
    private String createSign(String appKey, Map<String, Object> map) {

        /*JSONObject jsonObject=new JSONObject(true);
        jsonObject.putAll(map);
        String signStr = jsonObject.toString(SerializerFeature.WriteMapNullValue);*/
        String sign = "";
        try {
            //md5(mall_id+agentwe_mobile +name+mobile+company_na me+pid+tm+appkey)字
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, Object> m : map.entrySet()) {
                log.info("加密字段:{}:{}", m.getKey(), m.getValue());
                sb.append(m.getValue());
            }
            sb.append(appKey);
            sign = SecureUtil.md5(sb.toString());
            //sign = MD5Encrypt.getMessageDigest(MD5Encrypt.getMessageDigest(signStr + appKey) + appKey);
            //log.info("[签名]:字符串:{},签名:{}", signStr, sign);
        } catch (Exception e) {
            log.error("签名异常:" + e.getMessage());
        }
        return sign;
    }

    /**
     * 1000 申请成功
     * 1001 app_key不合法
     * 1002 mall_id不合法
     * 1004 进件姓名不合法
     * 1005 进件手机号不合法
     * 1006 公司名称不合法
     * 1007 产品ID不合法
     * 1008 签名不合法
     */
}
